<?php
class EmployeeSearch extends CFormModel {
	public $name;
	public $employed;
	public $status;
	public $employedDateStart;
	public $employedDateEnd;
	public $projectId;

	public function rules() {
		return array(
			array(
				'employedDateStart, employedDateEnd',
				'date',
				'format' => 'yyyy-MM-dd',
			),
			array(
				'name',
				'safe'
			),
			array(
				'employed, status, projectId',
				'numerical',
				'integerOnly' => true,
			),
		);
	}

	public function attributeLabels() {
		return array(
			'name' => '姓名',
			'employedDateStart' => '入职时间',
			'employed' => '在职员工',
			'projectId' => '所在项目',
			'status' => '状态'
		);
	}
	/**
	 * @return CDbCriteria
	 */
	public function createCriteria() {
		$criteria = new CDbCriteria;

		if ($this->employedDateStart) {
			$criteria->compare('employedDate', '>=' . $this->employedDateStart);
		}

		if ($this->employedDateEnd) {
			$criteria->compare('employedDate', '<=' . $this->employedDateEnd);
		}
		if ($this->employed!==null) {
			$criteria->compare('employed', $this->employed);
		}
		if ($this->status!==null) {
			$criteria->compare('status', $this->status);
		}
		if ($this->projectId>0) {
			$criteria->compare('currentProjectId', $this->projectId);
		}
		if ($this->name) {
			$criteria->addSearchCondition('CONCAT_WS(\'\',familyName, givenName)', $this->name);
		}
		$criteria->order = 'employedDate DESC';

		return $criteria;
	}
}
